﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.Xml;

namespace WindowsGame1.ItemClasses
{
    public class SatelliteEventArgs : EventArgs
    {
        Satellite sat;
        public SatelliteEventArgs(Satellite satellite)
        {
            sat = satellite;
        }

        public Satellite Satellite
        {
            get { return sat; }
            private set { sat = value; }
        }
    }


    public class ItemManager : DrawableGameComponent
    {
        public event SatelliteCollisionEventHandler SatelliteCollision;
        public delegate void SatelliteCollisionEventHandler(object sender,SatelliteEventArgs e);

        List<Satellite> satellites = new List<Satellite>();

        Dictionary<string, Weapon> weapons = new Dictionary<string, Weapon>();
        Dictionary<string, Armor> armors = new Dictionary<string, Armor>();
        Dictionary<string, Shield> shields = new Dictionary<string, Shield>();

        Random random = new Random();
        ContentManager Content;

        public ItemManager(Game game)
            : base(game)
        {
            Content = (ContentManager)Game.Services.GetService(typeof(ContentManager));
            Initialize();
        }

        public override void Initialize()
        {
            base.Initialize();
        }
        
        protected override void LoadContent()
        {
            ReadItems(@"Content\Items\items.its");
            CreateSatellites();
        }

        public override void Update(GameTime gameTime)
        {
            foreach (Satellite sat in satellites)
                sat.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            foreach (Satellite sat in satellites)
                sat.Draw(gameTime);
        }

        public void CheckSatelliteCollision(Vector2 origin)
        {
            for (int i = 0; i < satellites.Count; i++)
            {
                float distance = Vector2.Distance(satellites[i].Origin,origin);
                if (distance < Satellite.CollisionRadius)
                {
                    if (SatelliteCollision != null)
                    {
                        SatelliteEventArgs e = new SatelliteEventArgs(satellites[i]);
                        satellites.RemoveAt(i);
                        SatelliteCollision(this, e);
                    }
                }
            }
        }

        private void CreateSatellites()
        {
            Satellite tempSat;
            for (int i = 0; i < 200; i++)
            {
                tempSat = new Satellite(
                Game,
                Content.Load<Texture2D>(@"Items\chest"),
                new Vector2(random.Next(3, 100), random.Next(3, 100)),
                random.Next(100),
                random.Next(100, 200),
                null);
                satellites.Add(tempSat);
            }

            List<BaseItem> items = new List<BaseItem>();
            items.Add(weapons["dagger"]);
            items.Add(armors["leather armor"]);
            tempSat = new Satellite(
            Game,
            Content.Load<Texture2D>(@"Items\chest"),
            new Vector2(random.Next(5, 11), random.Next(5, 11)),
            random.Next(100),
            random.Next(100, 200),
            items);
            satellites.Add(tempSat);
            List<BaseItem> items2 = new List<BaseItem>();
            items2.Add(shields["buckler"]);
            tempSat = new Satellite(
            Game,
            Content.Load<Texture2D>(@"Items\chest"),
            new Vector2(random.Next(5, 11), random.Next(5, 11)),
            items2);
            satellites.Add(tempSat);
        }

        private void ReadItems(string filename)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(filename);
            XmlNode root = xmlDoc.FirstChild;
            weapons.Clear();
            armors.Clear();
            shields.Clear();
            foreach (XmlNode node in root.ChildNodes)
            {
                if (node.Name == "Weapons")
                    foreach (XmlNode wpn in node.ChildNodes)
                        ReadWeapon(wpn);
                if (node.Name == "Armors")
                    foreach (XmlNode arm in node.ChildNodes)
                        ReadArmor(arm);
                if (node.Name == "Shields")
                    foreach (XmlNode sld in node.ChildNodes)
                        ReadShield(sld);
            }
        }

        private void ReadWeapon(XmlNode wpn)
        {
            string name;
            int price;
            int weight;
            ItemSize size;
            int attackValue;
            int attackBonus;
            XmlNode node = wpn.FirstChild;
            if (node.Name != "Name")
                throw new Exception("Illegal file format!");
            name = node.Attributes[0].Value;
            node = node.NextSibling;
            if (node.Name != "Price")
                throw new Exception("Illegal file format!");
            price = int.Parse(node.Attributes[0].Value);
            node = node.NextSibling;
            if (node.Name != "Weight")
                throw new Exception("Illegal file format!");
            weight = int.Parse(node.Attributes[0].Value);
            node = node.NextSibling;
            if (node.Name != "Size")
                throw new Exception("Illegal file format!");
            size = (ItemSize)Enum.Parse(typeof(ItemSize),
            node.Attributes[0].Value);
            node = node.NextSibling;
            if (node.Name != "Attack")
                throw new Exception("Illegal file format!");
            attackValue = int.Parse(node.Attributes[0].Value);
            attackBonus = int.Parse(node.Attributes[1].Value);
            Weapon weapon = new Weapon(
            name,
            price,
            weight,
            size,
            attackValue,
            attackBonus);
            weapons.Add(name, weapon);
        }

        private void ReadArmor(XmlNode arm)
        {
            string name;
            int price;
            int weight;
            ItemSize size;
            int defenseValue;
            int defenseBonus;
            XmlNode node = arm.FirstChild;
            if (node.Name != "Name")
                throw new Exception("Illegal file format!");
            name = node.Attributes[0].Value;
            node = node.NextSibling;
            if (node.Name != "Price")
                throw new Exception("Illegal file format!");
            price = int.Parse(node.Attributes[0].Value);
            node = node.NextSibling;
            if (node.Name != "Weight")
                throw new Exception("Illegal file format!");
            weight = int.Parse(node.Attributes[0].Value);
            node = node.NextSibling;
            if (node.Name != "Size")
                throw new Exception("Illegal file format!");
            size = (ItemSize)Enum.Parse(typeof(ItemSize),
            node.Attributes[0].Value);
            node = node.NextSibling;
            if (node.Name != "Defense")
                throw new Exception("Illegal file format!");
            defenseValue = int.Parse(node.Attributes[0].Value);
            defenseBonus = int.Parse(node.Attributes[1].Value);
            Armor armr = new Armor(
            name,
            price,
            weight,
            size,
            defenseValue,
            defenseBonus);
            armors.Add(name, armr);
        }

        private void ReadShield(XmlNode sld)
        {
            string name;
            int price;
            int weight;
            ItemSize size;
            int defenseValue;
            int defenseBonus;
            XmlNode node = sld.FirstChild;
            if (node.Name != "Name")
                throw new Exception("Illegal file format!");
            name = node.Attributes[0].Value;
            node = node.NextSibling;
            if (node.Name != "Price")
                throw new Exception("Illegal file format!");
            price = int.Parse(node.Attributes[0].Value);
            node = node.NextSibling;
            if (node.Name != "Weight")
                throw new Exception("Illegal file format!");
            weight = int.Parse(node.Attributes[0].Value);
            node = node.NextSibling;
            if (node.Name != "Size")
                throw new Exception("Illegal file format!");
            size = (ItemSize)Enum.Parse(typeof(ItemSize),
            node.Attributes[0].Value);
            node = node.NextSibling;
            if (node.Name != "Defense")
                throw new Exception("Illegal file format!");
            defenseValue = int.Parse(node.Attributes[0].Value);
            defenseBonus = int.Parse(node.Attributes[1].Value);
            Shield shld = new Shield(
            name,
            price,
            weight,
            size,
            defenseValue,
            defenseBonus);
            shields.Add(name, shld);
        }
    }

}
